import React from 'react';
import { getInfo, getRouters } from '@/services/user';
import { listMenu } from '@/pages/System/Menu/service';
import { getToken } from '@/utils/auth';
import { history, RequestConfig, UseRequestProvider } from 'umi';

export async function getInitialState() {
  if (getToken()) {
    try {
      const userRouters = await listMenu();
      console.log('getInitialState');
      console.log(userRouters.data);
      return {
        userRouters: userRouters ? userRouters.data : [],
      };
    } catch (e) {
      return {};
    }
  }
  return {};
}

/** 修改交给 react-dom 渲染时的根组件 */
export function rootContainer(container: any) {
  const Provider = () => (
    <UseRequestProvider
      value={{
        initialData: [],
        formatResult: (res) => {
          console.log('root formatResult');
          console.log(res);
          return res && res.data ? res.data : [];
        },
      }}
    >
      {container}
    </UseRequestProvider>
  );
  return React.createElement(Provider, null, container);
}
